package com.sctvcloud.renhe.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.renhe.R;

/**
 * Created by Administrator on 2017/7/26 0026.
 */

public class MoreTextView extends LinearLayout {
    private View view;
    protected CustomFontTextView contentView; //文本正文
    protected LinearLayout expandView; //展开按钮
    protected CustomFontTextView expan_ct;
    protected ImageView point;

    //对应styleable中的属性
    protected int textColor;
    protected float textSize;
    protected int maxLine;
    protected String text;
    //默认属性值
    public int defaultTextColor = Color.BLACK;
    public int defaultTextSize = 12;
    public int defaultLine = 3;

    public MoreTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.moretext_layout, this);
        contentView = (CustomFontTextView) view.findViewById(R.id.moretext_layout_content);
        expandView = (LinearLayout) view.findViewById(R.id.moretext_layout_ll);
        expan_ct = (CustomFontTextView) view.findViewById(R.id.moretext_layout_all);
        point = (ImageView) view.findViewById(R.id.moretext_layout_point);
        // initalize(context);
        initWithAttrs(context, attrs);
        bindListener(context);
    }

    //初始化并添加View
    private void initalize(Context context) {
//        setOrientation(VERTICAL); //设置垂直布局
//        setGravity(Gravity.RIGHT); //右对齐
//        //初始化textView并添加
//        contentView = new CustomFontTextView(getContext());
//        addView(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//        //初始化ImageView并添加
//        expandView = new CustomFontTextView(getContext());
//        int padding = dip2px(getContext(), 5);
//        expandView.setPadding(padding, padding, padding, padding);
//        expandView.setText(context.getString(R.string.anchor_all));
//        expandView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
//        expandView.setTextSize(16);
//        //expandView.setImageResource(R.mipmap.ic_down);
//        LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        addView(expandView, llp);
    }

    private void initWithAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MoreTextStyle);
        int textColor = a.getColor(R.styleable.MoreTextStyle_textColor,
                defaultTextColor);  //取颜色值，默认defaultTextColor
        textSize = a.getDimensionPixelSize(R.styleable.MoreTextStyle_textSize, defaultTextSize);//取颜字体大小，默认defaultTextSize
        maxLine = a.getInt(R.styleable.MoreTextStyle_maxLine, defaultLine);//取颜显示行数，默认defaultLine
        Log.e("MoreTextView","---MAXlINES==" + maxLine);
        text = a.getString(R.styleable.MoreTextStyle_text);//取文本内容

        //绑定到textView
        bindTextView(textColor, textSize, maxLine, text);

        a.recycle();//回收释放
    }

    //绑定到textView
    protected void bindTextView(int color, float size, final int line, String text) {
        contentView.setTextColor(color);
        contentView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        contentView.setText(text);
        post(new Runnable() {//前面已讲，不再赘述
            @Override
            public void run() {
                int lineCount = contentView.getLineCount();
                if (lineCount > line) {
                    contentView.setHeight(contentView.getLineHeight() * line);
                } else {
                    contentView.setHeight(contentView.getLineHeight() * lineCount);
                }
                expandView.setVisibility(contentView.getLineCount() > line ? View.VISIBLE : View.GONE);

            }
        });
    }

    //点击展开与折叠
    private void bindListener(final Context context) {
        expandView.setOnClickListener(new OnClickListener() {
            boolean isExpand;

            @Override
            public void onClick(View v) {
                isExpand = !isExpand;
                contentView.clearAnimation();
                final int deltaValue;
                final int startValue = contentView.getHeight();
                int durationMillis = 350;
                if (isExpand) {
                    deltaValue = contentView.getLineHeight() * contentView.getLineCount() - startValue;
                    RotateAnimation animation = new RotateAnimation(0, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    expandView.startAnimation(animation);
                    expan_ct.setText(context.getString(R.string.anchor_retract));
                    point.setRotation(270.0f);
                } else {
                    deltaValue = contentView.getLineHeight() * maxLine - startValue;
                    RotateAnimation animation = new RotateAnimation(0, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(durationMillis);
                    animation.setFillAfter(true);
                    expandView.startAnimation(animation);
                    expan_ct.setText(context.getString(R.string.newes_see_all));
                    point.setRotation(90.0f);

                }
                Animation animation = new Animation() {
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        contentView.setHeight((int) (startValue + deltaValue * interpolatedTime));
                    }

                };
                animation.setDuration(durationMillis);
                contentView.startAnimation(animation);
            }
        });
    }

    public TextView getTextView() {
        return contentView;
    }

    public void setText(CharSequence charSequence) {
        contentView.setText(charSequence);
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
